package com.op.teachingliteapp.fragment.course;

import java.util.HashMap;
import java.util.Map;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;

import com.op.teachingliteapp.R;
import com.op.teachingliteapp.activity.CourseActivity;
import com.op.teachingliteapp.common.Common;
import com.op.teachingliteapp.util.ScreenSendUtils;
import com.op.teachingliteapp.util.SharedPreferenceUtil;

/**
 * 
 * @author handsome
 *
 */
public class CourseFragment extends Fragment {
	CourseGridAdapter mAdapter;
	private static final String TAG = CourseFragment.class.getSimpleName();
	private View root;// 用来传屏
	// private boolean isShowDeleted = false;//判断是否显示删除的课程的checkbox，后面会有用

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_course, container,
				false);
		final GridView mGridView = (GridView) rootView
				.findViewById(R.id.gridView);
		 mAdapter = new CourseGridAdapter(getActivity());
		mGridView.setAdapter(mAdapter);
		
		mGridView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					final int position, long id) {
				// if (isShowDeleted == false) {
				// isShowDeleted = true;
				// }
				// if (isShowDeleted == true) {
				// isShowDeleted = false;
				// }
				return true;
			}
		});

		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Map<String, Object> courseIdMap = new HashMap<String, Object>();
				courseIdMap.put(Common.Prefs.COURSE_ID, mAdapter.getCourseId(position));
				SharedPreferenceUtil.saveData(getActivity(), courseIdMap);
				/*
				 * 就是点击C语言的那个按钮 点击这里是从第一个界面跳转到第二个界面
				 */
				Intent i = new Intent(getActivity(), CourseActivity.class);
				startActivity(i);

			}
		});

		this.root = rootView;
		return rootView;
	}

	public void onResume() {
		super.onResume();
		Log.i(TAG, "onResume");
		ScreenSendUtils.sendWithDely(root);
		mAdapter.refresh();
	}

}
